package chapter04.part03;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

/**
 * 学习 URI & URL, 获取 Web 数据
 * 
 * URI 是一种语法, URL 是 URI 的一种实现
 * 
 * 使用 URLConnection 从某个 Web 网站获取资源
 * 1. 获得 URLConnection 对象
 * 2. 设置请求属性
 * 3. 连接
 * 4. 查询头信息
 * 5. 访问资源数据
 * 
 * @author insight
 * @since 2021/8/31
 */
public class URLConnectionTest {
    public static void main(String[] args) throws IOException {
        String urlName = "https://insight21.gitee.io/about";
        URL url = new URL(urlName);
        URLConnection connection = url.openConnection();
        
        // 仿造 UA, 才能获取网站内容
        connection.setRequestProperty("User-Agent",
                "Mozilla/5.0 (Windows NT 10.0) AppleWebKit/604.1.34 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/604.1");
        
        connection.connect();

        // 打印消息头中的字段和值
        Map<String, List<String>> headers = connection.getHeaderFields();

        for (Map.Entry<String, List<String>> entry :
                headers.entrySet()) {
            String key = entry.getKey();
            for (String v : entry.getValue()) {
                System.out.println(key + ": " + v);
            }
        }
        
        // 打印用于访问消息头的 6 个简便方法
        System.out.println("getContentType: " + connection.getContentType());
        System.out.println("getContentLength: " + connection.getContentLength());
        System.out.println("getContentEncoding: " + connection.getContentEncoding());
        System.out.println("getDate: " + connection.getDate());
        System.out.println("getExpiration: " + connection.getExpiration());
        System.out.println("getLastModified: " + connection.getLastModified());

        String encoding = connection.getContentEncoding();
        if (encoding == null) {
            encoding = "UTF-8";
        }
        try (Scanner in = new Scanner(connection.getInputStream(), encoding)) {
            // 打印头几行内容
            for (int i = 0; in.hasNextLine() && i < 11; i++) {
                System.out.println(in.nextLine());
            }
            if (in.hasNextLine()) System.out.println("......");
        }
    }
}